<!--
 * @Author: your name
 * @Date: 2020-03-26 09:41:08
 * @LastEditTime: 2020-03-26 09:41:10
 * @LastEditors: Please set LastEditors
 * @Description: 飞鱼购
 -->

<template>
  <div>
    <van-form class="flying-fish">
      <!-- 账号 -->
      <van-field
        v-model="ruleForm.phone"
        type="tel"
        maxlength="11"
        label="账号"
        placeholder="请填写账号"
        clearable
      />
      <!-- 密码 -->
      <van-field
        v-model="ruleForm.password"
        type="password"
        label="密码"
        placeholder="请填写密码"
        clearable
      />
      <!-- 验证码 -->
      <div class="popup-open__form-code">
        <van-field
          v-model="ruleForm.verify_code"
          type="tel"
          label="验证码"
          placeholder="请填写验证码"
          clearable
        />
        <van-image :src="imageSrc" @click="onGetCaptcha">
          <template v-slot:error>加载失败</template>
        </van-image>
      </div>
      <!-- 任务类型 -->
      <van-field v-model="IntervalTime" type="tel" label="抢单间隔(s)" placeholder="请填写抢单间隔" clearable />
      <van-field name="radio" label="任务类型">
        <template #input>
          <van-radio-group v-model="type" direction="horizontal">
            <van-radio
              v-for="(option, index) in typeOptions"
              :key="index"
              :name="option.type"
              checked-color="#e4393c"
            >{{ option.label }}</van-radio>
          </van-radio-group>
        </template>
      </van-field>
      <!-- 提示 -->
      <van-cell-group>
        <van-cell class="van-cell__tip" v-for="(content, index) in returnContent" :key="index" :title="content" />
      </van-cell-group>
    </van-form>
    <!-- 按钮 -->
    <div class="popup-open__btn">
      <van-button type="warning">做单</van-button>
      <van-button type="danger" @click="onStart">{{ startFlag ? '停止接单' : '开始接单' }}</van-button>
    </div>

    <!-- 选择账号 -->
    <van-dialog v-model="show" title="选择接单账号" :showConfirmButton="false" :showCancelButton="false">
      <van-cell v-for="(list, index) in accountList" :key="index" center :title="list.member_name">
        <template #right-icon>
          <van-switch
            v-model="list.is_open"
            active-color="#e4393c"
            size="24"
            @change="onChange(list)"
          />
        </template>
      </van-cell>
    </van-dialog>
  </div>
</template>

<script>
import dayjs from "dayjs";
import * as fish from "@/api/flying-fish";
export default {
  name: "FlyingFish",
  props: {
    index: {
      type: String,
      default: ''
    },
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      // 参数
      ruleForm: {
        phone: "18206488595",
        password: "123456",
        code_key: "",
        verify_code: "",
        phone_type: "i"
      },
      // 图形验证码
      imageSrc: "",
      // 循环时间
      IntervalTime: 8,
      // 任务类型
      type: "0",
      typeOptions: [
        { label: "当日单", type: "0" },
        { label: "流量单", type: "2" }
      ],
      // 开始登陆 标记
      startFlag: false,
      // 定时器
      timer: null,
      // 任务ID
      taskId: "",
      // 接口返回提示
      returnContent: [],
      // 已有平台
      iconItems: [],
      token: "",
      show: false,
      accountList: []
    };
  },
  watch: {
    // 监听接单状态
    startFlag() {
      this.iconItems =
        this.$store.getters.iconItems.length !== 0
          ? this.$store.getters.iconItems
          : this.$storage.get("iconItems");
          this.iconItems[this.index].opened = this.startFlag ? '已启动' : '待启动';
      this.$store.commit("SET_ICONITEMS", this.iconItems);
      this.$storage.set("iconItems", this.iconItems);
    },
    // 监听当前索引
    index() {
      if (this.index === 42) {
        this.onGetCaptcha();
      }
    },
    returnContent() {
      
      if (this.returnContent.length >= 10) {
        this.returnContent = this.returnContent.filter((item, i) => i < 5);
      }
    }
  },
  methods: {
    // 开始接单
    onStart() {
      this.onLogin();
    },
    // 登录
    onLogin() {
      const _this = this;
      if (!this.startFlag) {
         this.returnContent = [];
        this.startFlag = true;
        // 登录提示
        this.returnContent.unshift(
          dayjs().format("HH:mm:ss") + " " + "正在登录"
        );
        // 异步登录
        let param = {
          type: "post",
          data: this.ruleForm
        };
        fish.ajaxFun(fish.getLogin, param, res => {
          _this.returnContent.unshift(
            dayjs().format("HH:mm:ss") + " " + res.info
          );
          if (res.status === 1) {
            // 接单
            _this.token = res.data.login_token;
          } else {
            _this.startFlag = false;
            _this.onGetCaptcha();
          }
        });
      } else {
        this.returnContent.unshift(
          dayjs().format("HH:mm:ss") + " " + "接单取消"
        );
        this.$toast({
          message: "已停止接单",
          onClose: () => {
            clearInterval(_this.timer);
            _this.startFlag = false;
          }
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.popup-open__form-code {
  width: 100%;
  @include flex($flexWrap: nowrap);
  .van-image {
    flex: 0 0 100px;
    height: 44px;
  }
}
</style>